package com.base.api.service.ptGoldBean.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.base.exception.CustomException;
import com.base.self.mapper.PtgoldbeanMapper;
import com.base.self.entity.Commodity;
import com.base.self.entity.Ptgoldbean;
import com.base.self.entity.SelfOrder;
import com.base.self.enums.PtGoldbeanEnum;
import com.base.self.mapper.CommodityMapper;
import com.base.self.mapper.SelfOrderMapper;
import com.base.self.service.IPtgoldbeanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class IPtgoldbeanServiceImpl implements IPtgoldbeanService {

    @Autowired
    private PtgoldbeanMapper ptgoldbeanMapper;
    @Autowired
    private SelfOrderMapper orderMapper;
    @Autowired
    private CommodityMapper commodityMapper;

    @Override
    public Integer userGoldbeans(String userId) {
        Integer num = ptgoldbeanMapper.userGoldbeans(userId);
        return null==num?0:num;
    }

    @Override
    public Integer pt(String userId, String orderNo) {
        // 查询用户金豆数量
        Integer goldbeans = userGoldbeans(userId);
        if (0 == goldbeans){
            throw new CustomException("金豆不足,无法开启拼团!");
        }
        QueryWrapper<SelfOrder> wrapper = new QueryWrapper<SelfOrder>();
        wrapper.eq("order_no",orderNo);
        SelfOrder selfOrder = orderMapper.selectOne(wrapper);
        if (0 < selfOrder.getRealPrice().compareTo(new BigDecimal(goldbeans))){
            throw new CustomException("金豆不足,无法开启拼团!");
        }
        return 1;
    }

    @Override
    public Integer ptSubGoldBean(String userId, String orderNo) {
        QueryWrapper<SelfOrder> wrapper = new QueryWrapper<SelfOrder>();
        wrapper.eq("order_no",orderNo);
        SelfOrder selfOrder = orderMapper.selectOne(wrapper);
        return insert(PtGoldbeanEnum.SUB_PT,userId,selfOrder.getRealPrice());
    }

    @Override
    public Integer buyGift(String userId, String orderNo) {
        QueryWrapper<SelfOrder> wrapper = new QueryWrapper<SelfOrder>();
        wrapper.eq("order_no",orderNo);
        SelfOrder selfOrder = orderMapper.selectOne(wrapper);
        return insert(PtGoldbeanEnum.ADD_BUY_GIFT,userId,selfOrder.getRealPrice());
    }

/*    @Override
    public Integer exchangeGoods(String userId, String commodityId) {
        // 查询用户金豆数量
        Integer goldbeans = userGoldbeans(userId);
        if (0 == goldbeans){
            throw new CustomException("金豆不足,无法兑换!");
        }
        Commodity commodity = commodityMapper.selectById(commodityId);
        if (0 < commodity.getCommodityRealPrice().compareTo(new BigDecimal(goldbeans))){
            throw new CustomException("金豆不足,无法兑换!");
        }
        return insert(PtGoldbeanEnum.SUB_EXCHANGE_GIFT,userId,commodity.getCommodityRealPrice());
    }*/

    private Integer insert(PtGoldbeanEnum goldbeanEnum,String userId,BigDecimal money){
        Ptgoldbean ptgoldbean = new Ptgoldbean();
        ptgoldbean.setSub(userId);
        ptgoldbean.setType(0);
        ptgoldbean.setStatus(goldbeanEnum.getStatus());
        ptgoldbean.setContext(goldbeanEnum.getContext());
        ptgoldbean.setMoney(money);
        ptgoldbean.setCreateTime(new Date());
        return ptgoldbeanMapper.insert(ptgoldbean);
    }
}
